package com.silverhaze.diamond.common.event;

import com.silverhaze.commons.db.datasource.ObjectEntity;

/**
 * This code is part of Kg.Axion project. Treat as strictly confidential.
 * Author: Ivan Babanin
 * Creation date: 18.08.2010 13:11:59
 */
public class UIEventEntityChanged extends UIEvent {
    private Type type;
    private ObjectEntity entity;

    public UIEventEntityChanged(ObjectEntity entity, Type type) {
        this.type = type;
        this.entity = entity;
    }

    public ObjectEntity getEntity() {
        return entity;
    }

    @Override
    public boolean canMergeWith(UIEvent event) {
        return event instanceof UIEventEntityChanged && event.getChangedEntityClass().equals(getChangedEntityClass());
    }

    @Override
    public boolean isMergeable() {
        return true;
    }

    @Override
    public void merge(UIEvent event) {
    }

    @Override
    public Class<? extends ObjectEntity> getChangedEntityClass() {
        return entity.getClass();
    }

    public Type getType() {
        return type;
    }

    @Override
    public String toString() {
        return "UIEventEntityChanged{" +
                "type = " + type +
                ",\nentity = " + entity.toString() +
                '}';
    }

    public enum Type {
        PERSIST, UPDATE, DELETE
    }
}